package dao

import (
	"context"
	"github.com/exam/model"
	"github.com/go-redis/redis/v8"
	"github.com/goccy/go-json"
	"time"
)

type OrderRedis struct {
	client *redis.Client
}

func (o *OrderRedis) Set(ctx context.Context, data []model.Order, count int64, key string, orderKey string) {

	marshal, err := json.Marshal(count)
	if err != nil {
		return
	}
	orderdata, err := json.Marshal(data)
	if err != nil {
		return
	}
	expiration := time.Minute * 30
	o.client.Set(ctx, key, marshal, expiration)
	o.client.Set(ctx, orderKey, orderdata, expiration)
}
func (o *OrderRedis) GetCount(ctx context.Context, key string) (string, error) {
	result, err := o.client.Get(ctx, key).Result()
	if err != nil {
		return "", nil
	}
	return result, err
}
func (o *OrderRedis) GetOrderData(ctx context.Context, key string) (string, error) {
	result, err := o.client.Get(ctx, key).Result()
	if err != nil {
		return "", nil
	}
	return result, err
}
func NewOrderRedisClient(client *redis.Client) *OrderRedis {
	return &OrderRedis{client: client}
}
